package com.baor.controller;

import com.baor.config.TokenService;
import com.baor.fegin.ExceptionManagerFeignClient;
import com.baor.service.ExceptionReportService;
import com.baor.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/APP/exception")
public class ExceptionController {
    @Autowired
    private TokenService tokenService;

    @Autowired
    private ExceptionManagerFeignClient exceptionManagerFeignClient;

    @Autowired
    private ExceptionReportService exceptionReportService;

    //TODO 获取异常工单列表
    @RequestMapping("/list")
    public R getExceptionList(@RequestBody Map<String, Object> params) {
        return exceptionManagerFeignClient.getExceptionManagerInfo(params);
    }
    //TODO 获取详情
    @RequestMapping("/info/{id}")
    public R getExceptionInfo(@PathVariable("id") Integer id) {
        return exceptionManagerFeignClient.getInfo(id);
    }
    //TODO 上报处理结果
    @RequestMapping("/update")
    public R update(@RequestBody Map<String, Object> params) {
        return exceptionReportService.update(params);
    }

    //TODO 异常上报

    /**
     *  euqipmentId 设备ID
     *  abnormalDescription 异常描述
     *  abnormalType 异常类型
     * @param params
     * @return
     */
    @RequestMapping("/add")
    public R add(@RequestBody Map<String, Object> params,@RequestHeader(value = "token") String token) {
        if (token == null) {
            return R.er("未登录或token不存在");
        }
        Integer userId = tokenService.getUserId(token);
        if (userId == null) {
            return R.er("token无效");
        }
        params.put("reportUserId", userId);
        return exceptionReportService.insert(params);
    }

}
